<template> 
  <div>
    <el-dialog
      :title="dialogInfo.isAdd ? '添加轮播图' : '编辑轮播图'"
      :visible.sync="dialogInfo.isShow"
      :before-close="cancel"
      center
    >
      <el-form :model="bannerInfo" :rules="rules" ref="bannerInfo">
        <el-form-item label="标题" :label-width="formLabelWidth" prop="title">
          <el-input v-model="bannerInfo.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth">
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="onPreview"
            :on-remove="onRemove"
            :on-change="onChange"
            :auto-upload="false"
            :file-list="filelist"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt>
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="bannerInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="dialogInfo.isAdd" type="primary" @click="addBanner">添 加</el-button>
        <el-button v-else type="primary" @click="editBanner">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBannerAdd, getBannerEdit, getBannerInfo } from "../../request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  data() {
    return {
      bannerInfo: {
        id: 0, //分类编号，必填项
        title: "", //轮播图名称
        img: "", //图片(文件，一般用于二级分类)
        status: 1 //状态1正常2禁用
      },
      formLabelWidth: "120px", //label宽度
      //规则验证
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ],
      },
      //dialogVisible 预览图片是否显示
      dialogVisible: false,
      dialogImageUrl: "",
      filelist: []
    };
  },
  computed: {
    ...mapGetters({
      bannerlist: "banner/getBannerList"
    })
  },
  props: ["dialogInfo"],
  methods: {
    //文件改变时触发
    onChange(file, filelist) {
      // console.log(file, "333333");
      // console.log(filelist, "44444");
      this.bannerInfo.img = file.raw;
    },
    //图片预览方法
    onPreview(file) {
      console.log(file, "文件");
      this.dialogImageUrl = file.url;
      //让弹框显示
      this.dialogVisible = true;
      // console.log(this.dialogImageUrl,'this.dialogImageUrl');
    },
    //图片移除
    onRemove() {},
    ...mapActions({
      getBanner: "banner/getBannerListAction"
    }),
    //取消
    cancel() {
      //改变父组件的isShow的状态
      this.$emit("cancel", false);
      //调取清空事件
      this.reset();
    },
    //清空
    reset() {
      this.bannerInfo = {
        id: 0, //分类编号，必填项
        title: "", //轮播图名称
        img: "", //图片(文件，一般用于二级分类)
        status: 1 //状态1正常2禁用
      };
      //调用清空验证的方法
      this.$refs.bannerInfo.resetFields();
      //清空图片文件
      this.filelist = [];
    },
    // 确定事件
    addBanner() {
      this.$refs.bannerInfo.validate(valid => {
        if (valid) {
          //验证成功调用添加接口
          // console.log(this.cateInfo,'表单对象');
          getBannerAdd(this.bannerInfo).then(res => {
            console.log(res, "添加分类");
            if (res.code == 200) {
                console.log(res,"12345678")
              this.$message.success(res.msg);
              this.cancel();
              this.getBanner();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },
    //查询一条数据的接口
    lookup(id) {
      getBannerInfo({ id }).then(res => {
        if (res.code == 200) {
          this.bannerInfo = res.list;
          this.bannerInfo.id = id;
          //图片数据回显
          this.filelist = [{ url: this.$http + this.bannerInfo.img }];
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    // 编辑事件
    editBanner() {
      this.$refs.bannerInfo.validate(valid => {
        if (valid) {
          getBannerEdit(this.bannerInfo).then(res => {
            console.log(res, "添加分类");
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              this.getBanner();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    }
  }
};
</script>
<style scoped></style>
